<template>
  <div>
    用&nbsp;户&ensp;ID：<el-input v-model="form.uid" class="ipt" disabled></el-input>
    用户账号：<el-input v-model="form.account" class="ipt"></el-input><br />
    用&nbsp;户&nbsp;名&nbsp;：<el-input
      v-model="form.username"
      class="ipt"
    ></el-input>
    用户密码：<el-input v-model="form.password" class="ipt"></el-input><br />
    用户部门：<el-input v-model="form.dept" class="ipt"></el-input>
    用户状态：<el-input v-model="form.status" class="ipt" ></el-input
    ><br />
    <div class="btn">
      <el-button type="primary" @click="change">确认修改</el-button>
      <el-button type="danger" style="margin-left: 30px" @click="writeOff"
        >注销账号</el-button
      >
    </div>
  </div>
</template>

<script>
import axios from "axios";
axios.defaults.baseURL = "http://localhost:8888";
export default {
  name: "Onlyusertable",
  inject:["reload"],
  data() {
    return {
      form: {},
      form1: {},
      uid: window.sessionStorage.getItem("userId"),
    };
  },
  created() {
    this.load();
  },
  methods: {
    // 渲染所有的用户信息
    load() {
      var user = {
            uid:this.uid
        }
      axios.post(`user/selectUserById`, user).then((respose) => {
        console.log(respose.data.data);
        this.form = respose.data.data[0];
      });
    },
    // 修改消息
    change() {
      axios.put(`/user/updateUser`, this.form).then((response) => {
        if (response.data.status === 200) {
          this.$message({
            type: "success",
            message: "修改成功",
          });
          window.sessionStorage.setItem("username",response.data.data.username)
        } else {
          this.$message({
            type: "error",
            message: response.data.message,
          });
        }
        this.form = {};
        this.load();
      });
    },
    // 注销账号
    writeOff() {
      const msg = "您确认要删除吗";
      if (confirm(msg) == true) {
        axios.put(`user/closeUserById/${this.uid}`).then((response) => {
          if (response.data.status === 200) {
            this.$message({
              type: "success",
              message: "注销成功",
            });
          } else {
            this.$message({
              type: "error",
              message: response.data.message,
            });
          }
          this.form = {};
          this.$router.push("/login");
        });
      }
    },
  },
};
</script>

<style scoped>
.ipt {
  width: 35%;
  margin: 5px;
}
div {
  margin: 30px 30px;
  font-size: 14px;
  line-height: 28px;
  color: #939baa;
}
.btn {
  margin-left: 660px;
}
</style>